<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:04
 */

namespace common\business\empphoto;


use common\business\CommonHelper;
use common\business\DbHandler;
use common\business\upload\UploadHelper;
use common\consts\CommonResources;
use common\models\EmployeePhoto;
use common\models\Log;
use common\result\ActionResult;

class EmployeePhotoApiHandler extends DbHandler
{
    /**
     * @param $obj EmployeePhoto
     * @return ActionResult
     */
    public function createEmployeePhoto($obj){
        $fileExtArr = explode('/', $obj->image_content_type);
//        $uploadResult = UploadHelper::uploadEmployeePhotoFile($obj->employee_number, $obj->employee_name, $fileExtArr[1], $obj->image_data);
//        if (!$uploadResult){
//            return $uploadResult;
//        }
        $newLog = new Log([
            'action' => CommonResources::ACTION_NAME_MSG('Create', 'EmployeePhoto', CommonHelper::getPrintName($obj)),
        ]);
        return $this->create($obj, $newLog);
    }

    /**
     * @param EmployeePhoto $newObj
     * @param EmployeePhoto $oldObj
     * @return ActionResult
     */
    public function updateEmployeePhoto($newObj, $oldObj = null){
        if (!$oldObj){
            $oldObj = EmployeePhoto::findOne([
                'employee_number' => $newObj->employee_number,
            ]);
        }
//        $fileExtArr = explode('/', $newObj->image_content_type);
//        $uploadResult = UploadHelper::uploadEmployeePhotoFile($newObj->employee_number, $newObj->employee_name, $fileExtArr[1], $newObj->image_data);
//        if (!$uploadResult){
//            return $uploadResult;
//        }

        $updateLog = new Log([
            'action' => CommonResources::ACTION_NAME_MSG('Update', 'EmployeePhoto', CommonHelper::getPrintName($oldObj)),
        ]);
        return $this->update($newObj, $oldObj, [
            'employee_name',
            'image_name',
            'image_content_type',
            'image_url',
            'image_data',
            'image_width',
            'image_height',
            'image_source_url',
            'image_source_data',
            'memo',
        ], $updateLog);
    }
}